/**
 * All rights Reserved, Designed By www.ditingai.com
 *
 * @Title: DynamicProxyMain
 * @Package com.liufei.proxy
 * @author: 北京谛听机器人科技有限公司
 * @date: 2019/2/18 14:30
 * @version V1.0
 * @Copyright: 2019 www.ditingai.com . All rights reserved.
 * 注意：本内容仅限于北京谛听机器人科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.liufei.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 *Description TODO
 *@Author liufei
 *@DATE 2019/2/18 14:30
 **/
public class DynamicProxyMain {
    public static void consumer(Interface iface) {
        iface.getMyName();
        String name = iface.getNameById("1");
        System.out.println("name: " + name);
    }

    public static void main(String[] args) throws Exception, SecurityException, Throwable {
        RealObject realObject = new RealObject();
//        consumer(realObject);
//        System.out.println("==============================");

        // 动态代理
        ClassLoader classLoader = Interface.class.getClassLoader();
        Class<?>[] interfaces = new Class[] { Interface.class };
        InvocationHandler handler = new DynamicProxyHandler(realObject);
        Interface proxy = (Interface) Proxy.newProxyInstance(classLoader, interfaces, handler);

        System.out.println("in dynamicproxyMain proxy: " + proxy.getClass());
        consumer(proxy);
    }
}
